<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="Connect.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
<script language="javascript" type="text/javascript">
	function createClicked() {
		javascript: create_auction.submit();
	}
</script>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>Create An Auction</h1>
		<hr></hr>
		<%
			// Create a back button.
			out.println("<a href=\"view_items.jsp\" class=\"margin-5px button\">Back</a>");

			// Retrieve information for the item.
			String id = request.getParameter("id");
			if (id == null) {
				id = (String) session.getAttribute("item_id");
			}

			// Check if there were previously entered values for the form. If there aren't then use an
			// empty string.
			if (session.getAttribute("minimum_bid") == null) {
				session.setAttribute("minimum_bid", "");
			}
			if (session.getAttribute("bid_increment") == null) {
				session.setAttribute("bid_increment", "");
			}
			if (session.getAttribute("reserve_price") == null) {
				session.setAttribute("reserve_price", "");
			}
			if (session.getAttribute("end_date") == null) {
				session.setAttribute("end_date", "");
			}
			// Getting item information.
			java.sql.Connection conn = null;
			try {
				// Connect to the database.
				Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", ConnectionInfo.myUserID());
				sysprops.put("password", ConnectionInfo.myPassword());
				conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

				// Display a table of item information.
				java.sql.Statement statement = conn.createStatement();
				java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM items WHERE id='" + id + "'");
				java.sql.ResultSetMetaData columns = rs.getMetaData();
				if (rs.next()) {
		%>
		<table class="center item-table">
			<tr>
				<th class="left-text padding-5px">Item Id: <%=" " + rs.getString("id")%></th>
				<th></th>
			</tr>
			<tr>
				<%@ page import="java.io.File"%>
				<%
					String imagePath = application.getRealPath("item_images") + "\\" + rs.getString("id") + ".png";
							File file = new File(imagePath);
							if (!file.exists()) {
								//imagePath = application.getRealPath("item_images") + "\\" + "0" + ".png";
								imagePath = "images/logo.png";
							} else {
								// Runtime correctioin. Mostly has to do with local servers.
								imagePath = imagePath.substring(imagePath.indexOf("item_images"));
							}
				%>
				<td style="vertical-align: top" width="128" height="128"><img
					src="<%=imagePath%>" alt="item image" width="128" height="128"></td>
				<td>
					<form name="create_auction" action="create_auction_validate.jsp"
						method="post">
						<table class="inner-table">
							<tr>
								<th>Name:</th>
								<td><%=" " + rs.getString("name")%></td>
							</tr>
							<tr>
								<th>Year:</th>
								<td><%=" " + rs.getString("year")%></td>
							</tr>
							<tr>
								<th>Type:</th>
								<td><%=" " + rs.getString("type")%></td>
							</tr>
							<tr>
								<th>Copies Available:</th>
								<td><%=" " + rs.getString("copies_available")%></td>
							</tr>
							<tr>
								<th>Copies Sold:</th>
								<td><%=" " + rs.getString("copies_sold")%></td>
							</tr>
							<tr>
								<th>Description:</th>
								<td><%=" " + rs.getString("description")%></td>
							</tr>
							<tr>
								<td></td>
								<td><br />
									<h2>Enter Auction Details</h2>
									<hr></hr> <%
 	if (session.getAttribute("register_errors") != null) {
 				out.println("<div class=\"section padding-5px error-text\">");
 				out.println(session.getAttribute("register_errors"));
 				out.println("</div>");
 			}
 %>
									<table class="center full-width">
										<tr>
											<td class="right"><b>Minimum Bid:</b></td>
											<td><input name="minimum_bid" type="text"
												value="<%=session.getAttribute("minimum_bid")%>" /></td>
										</tr>
										<tr>
											<td class="right"><b>Bid Increment:</b></td>
											<td><input name="bid_increment" type="text"
												value="<%=session.getAttribute("bid_increment")%>" /></td>
										</tr>
										<tr>
											<td class="right"><b>Reserve Price:</b></td>
											<td><input name="reserve_price" type="text"
												value="<%=session.getAttribute("reserve_price")%>" /></td>
										</tr>
										<tr>
											<td class="right"><b>End Date:</b></td>
											<td><input name="end_date" type="text"
												value="<%=session.getAttribute("end_date")%>" /></td>
										</tr>
										<tr>
											<td></td>
											<td><a href="#" class="right button"
												onclick="return createClicked()">Create Auction</a></td>
										</tr>
									</table></td>
							</tr>
						</table>
						<input name="item_id" type="hidden" value="<%=id%>" />
					</form>
				</td>
			</tr>
		</table>
		<br />
		<%
			}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					conn.close();
				} catch (Exception ee) {
				}
			}

			// Clear all the attributes after they have been read.
			session.setAttribute("register_errors", null);
			//session.setAttribute("item_id", null);
			session.setAttribute("minimum_bid", null);
			session.setAttribute("bid_increment", null);
			session.setAttribute("reserve_price", null);
			session.setAttribute("end_date", null);
		%>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>